package ru.dc.object.commands;

/**
 * <h3>Syntax</h3>
 * <p/>
 * <p>Older clients, DC++ and NMDC protocol compatible clients:</p>
 * <pre>
 * $ConnectToMe &lt;RemoteNick> &lt;SenderIp>:&lt;SenderPort>
 * </pre>
 * <p/>
 * <p>For example:</p>
 * <pre>
 * $ConnectToMe RemoteNick 213.235.184.138:19346
 * </pre>
 * <p/>
 * <p>NMDC v2.205 and DC:PRO v0.2.3.97A:</p>
 * <pre>
 * $ConnectToMe &lt;SenderNick> &lt;RemoteNick> &lt;SenderIp>:&lt;SenderPort>
 * </pre>
 * <p>For example:</p>
 * <pre>$ConnectToMe MyNick ThatNick 83.43.232.34:1412</pre>
 * <p/>
 * <h3>Description</h3>
 * <p> This command starts the process for a client-client connection initiated by an active mode client.
 * On receiving, the client responds by connecting to the IP and port number specified, and from there the
 * client-client protocol takes over.</p>
 * <p/>
 * <p>SenderNick is active and wants to connect to RemoteNick. SenderNick sends through the hub,
 * and starts to listen for incoming connection at address Client1IP:Client1port (TCP).</p>
 */
public class ConnectToMe extends Command {

    private String senderNick;
    private String nick;
    private String address;
    private int port;

    @Override
    public String generateCommand() {
        return getActualCommandName()+" "+
//                (getSenderNick()!=null? getSenderNick()+" " : "")+
                getNick()+" "+getAddress()+":"+getPort();
    }

    @Override
    public String getActualCommandName() {
        return "$ConnectToMe";
    }

    @Override
    public void parseCommand() {
        String tokensSubstr = getCommand().substring(getActualCommandName().length()+1);
        String[] tokens = tokensSubstr.split(" ");
        if (tokens.length>=3) {
            setSenderNick(tokens[0]);
            setNick(tokens[1]);
        }
        else {
            setNick(tokens[0]);
        }

        String[] addrPort = tokens[tokens.length-1].split(":");
        setAddress(addrPort[0]);
        String portStr = addrPort[1].replaceAll("\\D", "");
        setPort(new Integer(portStr));
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    public String getSenderNick() {
        return senderNick;
    }

    public void setSenderNick(String senderNick) {
        this.senderNick = senderNick;
    }
}
